package core;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.event.*;
import javax.swing.text.*;

import java.util.*;

public class MainWindow extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	private Regex r;
	
	// textfield
	private JTextField textfield;
	
	// textpane
	private JTextPane textpane;
	private StyleContext sc;
	private Style s;
	private DefaultStyledDocument doc;
	
	// listeners
	private TextFieldListener textFieldListener;
	private TextPaneListener textPaneListener;
	private boolean textFieldListenerInAction;
	
	/*
	private CheckboxGroup group;
	private Checkbox cbMatch;
	private Checkbox cbFind;
	*/

	MainWindow() throws Exception {
		super();
		this.setTitle("JRegexBoy");
        this.setResizable(false);
        this.getContentPane().setLayout(null);
        
        /*
        group = new CheckboxGroup();
        cbMatch = new Checkbox("match", true, group);
        cbFind	= new Checkbox("find", false, group);
        */
        
        // textfield regex
        textfield = new JTextField("\\w*o\\w*");
        textfield.setFont(new Font("Monospaced", Font.PLAIN, 16));
        textfield.setForeground(new Color(0,0,0));
        textfield.setBounds(1, 2, 596, 40);
        
        
		// jtextpane text
		sc = new StyleContext(); 
		doc = new DefaultStyledDocument(sc); //using that style context creating styled document
		textpane=new JTextPane(doc); //using that document creating textpane

		//creating a style 
		s = sc.addStyle(null, null);
		StyleConstants.setForeground(s,Color.black);
		StyleConstants.setFontFamily(s, "Monospaced");
		StyleConstants.setFontSize(s, 16);
		doc.insertString(0,"The quick ",s); //applying the style to the document text
		StyleConstants.setForeground(s,Color.blue);
		doc.insertString(doc.getLength(),"brown fox ",s);
		StyleConstants.setForeground(s,Color.black);
		doc.insertString(doc.getLength(),"jumps ",s);
		StyleConstants.setForeground(s,Color.blue);
		doc.insertString(doc.getLength(),"over ",s);
		StyleConstants.setForeground(s,Color.black);
		doc.insertString(doc.getLength(),"the lazy ",s);
		StyleConstants.setForeground(s,Color.blue);
		doc.insertString(doc.getLength(),"dog",s);
		
        // adds
        this.getContentPane().add(textfield);
    	JScrollPane jscrollpane = new JScrollPane(textpane);
    	jscrollpane.setBounds(4, 44, 590, 300);
    	
        this.getContentPane().add(jscrollpane);
        //this.add(cbMatch, null);
        //this.add(cbFind, null);
        
        // listener
        textFieldListener = new TextFieldListener();
        textfield.getDocument().addDocumentListener(textFieldListener);
        textFieldListenerInAction=false;
        
        textPaneListener = new TextPaneListener();
        textpane.getDocument().addDocumentListener(textPaneListener);
        
        this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        
        // regex
        r = new Regex();
	}

	class TextFieldListener implements DocumentListener {
	 
	    public void changedUpdate(DocumentEvent e) {
	        //Plain text components do not fire these events
	    }

		public void insertUpdate(DocumentEvent e) {
			//System.out.println("TextFieldListener");
			textFieldListenerInAction=true;
			checkRegex();		
			textFieldListenerInAction=false;
		}

		public void removeUpdate(DocumentEvent e) {
			//System.out.println("TextFieldListener");
			textFieldListenerInAction=true;
			checkRegex();		
			textFieldListenerInAction=false;		
		}

		public void checkRegex() {
			java.util.List<String> parts = r.getParts(textfield.getText(), textpane.getText());
        	
    		if (parts != null) {

    			textpane.setText("");
	        	boolean mustblack = true;
	        	
	        	Iterator<String> i = parts.iterator();
	        	while (i.hasNext()) { 
	        		String part = i.next();
	        		
	        		if (mustblack) {
	        			StyleConstants.setForeground(s, Color.black);
	        			mustblack = false;
	        		} else {
	        			StyleConstants.setForeground(s,Color.blue);
	        			mustblack = true;
	        		}
	        		
	        		try {
	        			doc.insertString(doc.getLength(),part,s);
					} catch (BadLocationException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
	        	} // while
    		} // if
		} // regex
	} // innerclass
	
	
	class TextPaneListener implements DocumentListener {

		public void changedUpdate(DocumentEvent e) {
			// TODO Auto-generated method stub
			
		}

		public void insertUpdate(DocumentEvent e) {
			if (!textFieldListenerInAction) {
				//System.out.println("TextPaneListener");
				checkRegex();		
			}
		}

		public void removeUpdate(DocumentEvent e) {
			if (!textFieldListenerInAction) {
				//System.out.println("TextPaneListener");
				checkRegex();		
			}
		}
		
	    public void checkRegex() {
	    	
	    	final java.util.List<String> parts = r.getParts(textfield.getText(), textpane.getText());
	        final DocumentListener listener = this;
	        
	        javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        textpane.getDocument().removeDocumentListener(listener);
                    	
                		if (parts != null) {

                			int c = textpane.getCaretPosition();
                			
                			textpane.setText("");
            	        	boolean mustblack = true;
            	        	
            	        	Iterator<String> i = parts.iterator();
            	        	while (i.hasNext()) { 
            	        		String part = i.next();
            	        		
            	        		if (mustblack) {
            	        			StyleConstants.setForeground(s, Color.black);
            	        			mustblack = false;
            	        		} else {
            	        			StyleConstants.setForeground(s,Color.blue);
            	        			mustblack = true;
            	        		}
            	        		
            	        		try {
            	        			doc.insertString(doc.getLength(),part,s);
            					} catch (BadLocationException e1) {
            						// TODO Auto-generated catch block
            						e1.printStackTrace();
            					}
            	        	} // while
            	        			
                			textpane.setCaretPosition(c);
                		} // if
                        
                        textpane.getDocument().addDocumentListener(listener);
                    }
                });
	    }
	}


} 